package com.zbkj.admin.oss.oss.http;

import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import com.zbkj.admin.oss.core.FileProperties;
import com.zbkj.admin.oss.oss.OssProperties;
import com.zbkj.admin.oss.oss.service.OssTemplate;
import lombok.AllArgsConstructor;
import lombok.Cleanup;
import lombok.SneakyThrows;
import net.dreamlu.mica.core.result.R;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * aws 对外提供服务端点
 *
 * @author lengleng
 * @author 858695266
 * <p>
 * oss.info
 */
@RestController
@AllArgsConstructor
@RequestMapping("/oss")
//@ConditionalOnProperty(name = "file.oss.info", havingValue = "true")
public class OssEndpoint {

    private final OssTemplate template;
    private final FileProperties ossProperties;

    /**
     * Bucket Endpoints
     */
    @PostMapping("/bucket/{bucketName}")
    public Bucket createBucker(@PathVariable String bucketName) {

        template.createBucket(bucketName);
        return template.getBucket(bucketName).get();

    }

    @GetMapping("/page")
    public R<Map<String, Object>> page(@RequestParam("attType") Set<String> imageTypes,
                                       @RequestParam("page") Integer page,
                                       @RequestParam(value = "nextToken", required = false) String pageSize) {
        return R.success(template.getImageListPage("app", null, imageTypes, page, String.valueOf(pageSize)));
    }

    @GetMapping("/bucket")
    public List<Bucket> getBuckets() {
        return template.getAllBuckets();
    }

    @GetMapping("/bucket/{bucketName}")
    public Bucket getBucket(@PathVariable String bucketName) {
        return template.getBucket(bucketName).orElseThrow(() -> new IllegalArgumentException("Bucket Name not found!"));
    }

    @DeleteMapping("/bucket/{bucketName}")
    @ResponseStatus(HttpStatus.ACCEPTED)
    public void deleteBucket(@PathVariable String bucketName) {
        template.removeBucket(bucketName);
    }


//    @GetMapping("/object/{bucketName}/{objectName}")
//    public List<S3ObjectSummary> getList(@PathVariable("bucketName") String bucketName,
//                                         @RequestParam("page")Integer page,
//                                         @RequestParam("attType")Integer imageTypes,
//                                         @RequestParam("limit")Integer limit){
//        return template.getImageList(bucketName, null, null, limit);
//    }

    /**
     * Object Endpoints
     */
    @PostMapping("/object/{bucketName}")
    public R<String> createObject(@RequestParam("multipart") MultipartFile multipart, @PathVariable("bucketName") String bucketName) throws Exception {
        String name = multipart.getOriginalFilename();
        @Cleanup
        InputStream inputStream = multipart.getInputStream();
        template.putObject(bucketName, name, inputStream, multipart.getSize(), multipart.getContentType());
        return R.success(ossProperties.getOss().getEndpoint() + "/" + bucketName + "/" + name);

    }

    @PostMapping("/object/{bucketName}/{objectName}")
    public S3Object createObject(@RequestBody MultipartFile object, @PathVariable String bucketName,
                                 @PathVariable String objectName) throws Exception {
        @Cleanup
        InputStream inputStream = object.getInputStream();
        template.putObject(bucketName, objectName, inputStream, object.getSize(), object.getContentType());
        return template.getObjectInfo(bucketName, objectName);

    }

    @GetMapping("/object/{bucketName}/{objectName}")
    public List<S3ObjectSummary> filterObject(@PathVariable String bucketName, @PathVariable String objectName) {

        return template.getAllObjectsByPrefix(bucketName, objectName, true);

    }

    @GetMapping("/object/{bucketName}/{objectName}/{expires}")
    public Map<String, Object> getObject(@PathVariable String bucketName, @PathVariable String objectName,
                                         @PathVariable Integer expires) {
        Map<String, Object> responseBody = new HashMap<>(8);
        // Put Object info
        responseBody.put("bucket", bucketName);
        responseBody.put("object", objectName);
        responseBody.put("url", template.getObjectURL(bucketName, objectName, expires));
        responseBody.put("expires", expires);
        return responseBody;
    }

    @ResponseStatus(HttpStatus.ACCEPTED)
    @DeleteMapping("/object/{bucketName}/{objectName}/")
    public void deleteObject(@PathVariable String bucketName, @PathVariable String objectName) throws Exception {

        template.removeObject(bucketName, objectName);
    }

}
